﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepairChecker
{
    public partial class schedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }
            
                

        }

        void RenderUI()
        {
            List<District> districts = Entity.GetDistricts(true);
            lstDistrict.Items.Clear();
            lstDistrict.Items.Add("");
            foreach (District d in districts)
            {
                lstDistrict.Items.Add(new ListItem(d.DistrictName, d.DistrictId.ToString()));
            }

        }

        //Export to Excel from a GridView
        protected void ExportToExcel()
        {
            string fileName = "派单表" + DateTime.Now.ToShortDateString();

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AddHeader("content-disposition", "attachment;filename="+fileName+".xls");
            Response.Charset = "";
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            dataSchedule.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //should be reserved
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }

        protected void lstDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Office> offices = Entity.GetOffices(int.Parse(lstDistrict.SelectedValue), true);
            lstOffice.Items.Clear();
            lstOffice.Items.Add("");
            foreach (Office o in offices)
            {
                lstOffice.Items.Add(new ListItem(o.OfficeName, o.OfficeId.ToString()));
            }
            dataSchedule.DataBind();
        }

        protected void btnAddMaintaince_Click(object sender, EventArgs e)
        {
            //主动维护 faultid=7
            if (lstOffice.SelectedValue == "")
            {
                lblError.Text = "选择网点";
                return;
            }

            int ret = 0;
                Entity.AddJob(int.Parse(lstOffice.SelectedValue),"7," , Entity.JobType.Maintain,"");
            if (ret == -1)
            {
                lblError.Text = "提交出错";
            }
            else
            {
                dataSchedule.DataBind();
                RenderUI();
                lstOffice.Items.Clear();
                lstDistrict.SelectedIndex = 0;
                //lstOffice.SelectedIndex = 0;
            }
        }

        protected void dataSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Job job = (Job)e.Row.DataItem;

                if (!String.IsNullOrEmpty(job.FaultIdString))
                {
                    BulletedList _lstFaults = (BulletedList)e.Row.FindControl("lstFaults");
                    List<Fault> faults = Entity.GetAllFaults(true);

                    foreach (string s in job.FaultIdString.Split(','))
                    {
                        if (!string.IsNullOrEmpty(s)) //
                        {
                            foreach (Fault f in faults)
                            {
                                if (f.FaultId == int.Parse(s))
                                {
                                    _lstFaults.Items.Add(f.FaultName);
                                    break;
                                }
                            }
                        }

                    }
                }
            }
        }
    }
}